<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="扣款订单编码" align="center" prop="deductionOrderRecordCode" />
      <el-table-column label="客户名称" align="center" prop="consumerName" />
      <el-table-column label="客户电话" align="center" prop="consumerPhone" />
      <el-table-column label="扣款金额" align="center" prop="deductionAmount" />
      <el-table-column label="业务单号" align="center" prop="businessNo" />
      <el-table-column label="是否分账" align="center" prop="isAlt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_alt" :value="scope.row.isAlt"/>
        </template>
      </el-table-column>
      <el-table-column label="分账类型" align="center" prop="altType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alt_type" :value="scope.row.altType"/>
        </template>
      </el-table-column>
      <el-table-column label="分账商户" align="center" prop="altInfo" />
      <el-table-column label="扣款状态" align="center" prop="deductionStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.deduction_status" :value="scope.row.deductionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <!--            v-hasPermi="['deduction:record:edit']"-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
          <!--            v-hasPermi="['deduction:record:remove']"-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改扣款请求日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="扣款订单记录code" prop="deductionOrderRecordCode">
          <el-input v-model="form.deductionOrderRecordCode" placeholder="请输入扣款订单记录code" />
        </el-form-item>
        <el-form-item label="扣款订单id" prop="deductionOrderId">
          <el-input v-model="form.deductionOrderId" placeholder="请输入扣款订单id" />
        </el-form-item>
        <el-form-item label="客户id" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="客户名称" prop="consumerName">
          <el-input v-model="form.consumerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="客户电话" prop="consumerPhone">
          <el-input v-model="form.consumerPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="扣款金额" prop="deductionAmount">
          <el-input v-model="form.deductionAmount" placeholder="请输入扣款金额" />
        </el-form-item>
        <el-form-item label="业务单号" prop="businessNo">
          <el-input v-model="form.businessNo" placeholder="请输入业务单号" />
        </el-form-item>
        <el-form-item label="业务回调地址" prop="businessCallbackUrlDeduction">
          <el-input v-model="form.businessCallbackUrlDeduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="业务回调地址" prop="businessCallbackUrlAlt">
          <el-input v-model="form.businessCallbackUrlAlt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="平台回调地址" prop="callbackUrlDeduction">
          <el-input v-model="form.callbackUrlDeduction" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="平台回调地址" prop="callbackUrlAlt">
          <el-input v-model="form.callbackUrlAlt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否分账" prop="isAlt">
          <el-input v-model="form.isAlt" placeholder="请输入是否分账" />
        </el-form-item>
        <el-form-item label="分账类型" prop="altType">
          <el-select v-model="form.altType" placeholder="请选择分账类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="分账商户号列表，逗号分隔" prop="altInfo">
          <el-input v-model="form.altInfo" placeholder="请输入分账商户号列表，逗号分隔" />
        </el-form-item>
        <el-form-item label="商户ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入商户ID" />
        </el-form-item>
        <el-form-item label="商户code" prop="tenantCode">
          <el-input v-model="form.tenantCode" placeholder="请输入商户code" />
        </el-form-item>
        <el-form-item label="商户名" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入商户名" />
        </el-form-item>
        <el-form-item label="扣款请求记录状态">
          <el-radio-group v-model="form.deductionStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/deduction/record";

export default {
  name: "Record",
  dicts: ['is_alt','alt_type','deduction_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 扣款请求日志表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deductionOrderRecordCode: null,
        deductionOrderId: null,
        consumerId: null,
        consumerName: null,
        consumerPhone: null,
        deductionAmount: null,
        businessNo: null,
        businessCallbackUrlDeduction: null,
        businessCallbackUrlAlt: null,
        callbackUrlDeduction: null,
        callbackUrlAlt: null,
        isAlt: null,
        altType: null,
        altInfo: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
        deductionStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deductionOrderRecordCode: [
          { required: true, message: "扣款订单记录code不能为空", trigger: "blur" }
        ],
        deductionOrderId: [
          { required: true, message: "扣款订单id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询扣款请求日志列表 */
    getList() {
      this.loading = true;
      const deductionOrderId = this.$route.params && this.$route.params.deductionOrderId;
      if(deductionOrderId){
        this.queryParams.deductionOrderId = deductionOrderId;
      }
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deductionOrderRecordId: null,
        deductionOrderRecordCode: null,
        deductionOrderId: null,
        consumerId: null,
        consumerName: null,
        consumerPhone: null,
        deductionAmount: null,
        businessNo: null,
        businessCallbackUrlDeduction: null,
        businessCallbackUrlAlt: null,
        callbackUrlDeduction: null,
        callbackUrlAlt: null,
        isAlt: null,
        altType: null,
        altInfo: null,
        tenantId: null,
        tenantCode: null,
        tenantName: null,
        deductionStatus: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deductionOrderRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加扣款请求日志";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      const deductionOrderRecordId = row.deductionOrderRecordId;
      this.$router.push("/deduction/threeDetail/orderDetail/" + deductionOrderRecordId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deductionOrderRecordId = row.deductionOrderRecordId || this.ids
      getRecord(deductionOrderRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改扣款请求日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deductionOrderRecordId != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deductionOrderRecordIds = row.deductionOrderRecordId || this.ids;
      this.$modal.confirm('是否确认删除扣款请求日志编号为"' + deductionOrderRecordIds + '"的数据项？').then(function() {
        return delRecord(deductionOrderRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有扣款请求日志数据项？').then(() => {
        this.exportLoading = true;
        return exportRecord(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
